package com.thf.service.impl;

import com.thf.dao.UserDao;
import com.thf.model.PageModel;
import com.thf.model.ResultData;
import com.thf.model.UserModel;
import com.thf.service.UserService;
import com.thf.util.ResultTool;
import com.thf.util.Tool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Override
    public ResultData login(UserModel userModel) {
        UserModel model = userDao.login(userModel.getUsername(),userModel.getPassword());
        if(model ==null){
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_ERROR_MSG);
        }
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_SUCCES_MSG);
    }

    @Override
    public ResultData userFindAll(PageModel pageModel) {
        List<UserModel> list =  userDao.findAll(pageModel);
        if(list ==null){
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_ERROR_MSG);
        }
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_SUCCES_MSG,list);
    }

    @Override
    public ResultData userFind(PageModel pageModel, Map map) {
        UserModel userModel = new UserModel(Tool.jsonToString(map.get("username")), Tool.jsonToString(map.get("sex"))
                , Tool.jsonToString(map.get("phone")), Tool.jsonToString(map.get("hobby"))
                , Tool.jsonToString(map.get("createTime")), Tool.jsonToString(map.get("updateTime")));
        List<UserModel> list =null ;
        if(userModel!=null){
            String[] hobbys = userModel.getHobby().split(",");
            list =  userDao.find(pageModel,userModel,hobbys);
        }
        if(list ==null){
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_ERROR_MSG);
        }
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_SUCCES_MSG,list);
    }
//
//    @Override
//    public Integer getCount() {
//        UserDao userDao = new UserDaoImpl();
//        Integer count = userDao.getCount();
//        return count;
//    }
    @Override
    public Integer getCount(Map map) {
        UserModel userModel = new UserModel(Tool.jsonToString(map.get("username")), Tool.jsonToString(map.get("sex"))
                , Tool.jsonToString(map.get("phone")), Tool.jsonToString(map.get("hobby"))
                , Tool.jsonToString(map.get("createTime")), Tool.jsonToString(map.get("updateTime")));
        Integer count = null ;
        if(userModel!=null){
            String[] hobbys = userModel.getHobby().split(",");
            count = userDao.getCount(userModel,hobbys);
        }
        return count;
    }

    @Override
    public ResultData add(Map map) {
        UserModel userModel = new UserModel(Tool.jsonToString(map.get("username")), Tool.jsonToString(map.get("pass"))
                , Tool.jsonToString(map.get("sex")), Tool.jsonToString(map.get("birthday"))
                , Tool.jsonToString(map.get("phone")), Tool.jsonToString(map.get("address"))
        , Tool.jsonToString(map.get("hobby")), Tool.jsonToString(map.get("email")), Tool.jsonToString(map.get("enable"))
        , Tool.jsonToString(map.get("remark")),Tool.dateToString(new Date()));
        Integer count = null ;
        if(userModel!=null){
            count = userDao.add(userModel);
        }
        if(count ==0){
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_ADD_ERROR_MSG);
        }
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_ADD_SUCCES_MSG,count);
    }

    @Override
    public ResultData delete(Integer uid) {
        Integer count = null ;
        if(uid != null){
            count = userDao.delete(uid);
        }
        if(count ==0){
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_DEL_ERROR_MSG);
        }
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_DEL_SUCCES_MSG,count);
    }

    @Override
    public ResultData getUserById(Integer uid) {
        UserModel userModel = null;
        if(uid != null){
            userModel = userDao.getUserById(uid);
        }
        if(userModel == null){
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_ID_ERROR_MSG);
        }
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_ID_SUCCES_MSG,userModel);
    }

    @Override
    public ResultData update(Map map) {
        UserModel userModel = new UserModel();
        userModel.setUsername(Tool.jsonToString(map.get("username")));
        userModel.setPassword(Tool.jsonToString(map.get("pass")));
        userModel.setSex(Tool.jsonToString(map.get("sex")));
        userModel.setBirthday(Tool.jsonToString(map.get("birthday")));
        userModel.setPhone(Tool.jsonToString(map.get("phone")));
        userModel.setAddress(Tool.jsonToString(map.get("address")));
        userModel.setHobby(Tool.jsonToString(map.get("hobby")));
        userModel.setId((Integer) map.get("id"));
        userModel.setEmail(Tool.jsonToString(map.get("email")));
        userModel.setEnable(Tool.jsonToString(map.get("enable")));
        userModel.setRemark(Tool.jsonToString(map.get("remark")));
        userModel.setUpdateTime(Tool.dateToString(new Date()));
        System.out.println(userModel.toString());
        Integer count = null ;
        if(userModel!=null){
            count = userDao.update(userModel);
        }
        if(count ==0){
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_UPDATE_ERROR_MSG);
        }
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_UPDATE_SUCCES_MSG,count);
    }

    @Override
    public ResultData updateEnadle(String id) {
        Integer uid = Tool.stringToInt(id);
        return new ResultData(userDao.updateEnable(uid) > 0 ? ResultTool.CODE_SUCCES : ResultTool.CODE_ERROR ,"");
    }
}
